package com.lin.forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

@Data
public class Result<T> {
    //状态码
    @JsonInclude(JsonInclude.Include.ALWAYS)//不论任何情况都参与JSON序列化
    int code;
    //描述信息
    @JsonInclude(JsonInclude.Include.ALWAYS)//不论任何情况都参与JSON序列化
    private String message;
    @JsonInclude(JsonInclude.Include.ALWAYS)//不论任何情况都参与JSON序列化
    private T data;
    /**
     *成功
     */
    public static Result success(){
        Result result=new Result();
        result.setCode(ResultCodeEnum.SUCCESS.code);
        result.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return result;
    }
    public static Result success(int code,String message){
        Result result=new Result();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }
    public static<T> Result<T> success(T data){
        Result result=new Result();
        result.setCode(ResultCodeEnum.SUCCESS.code);
        result.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }
    public static<T> Result<T> success(String message,T data){
        Result result=new Result();
        result.setCode(ResultCodeEnum.SUCCESS.code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }
    /**
     * 失败
     */
    public static Result fail(){
        Result result=new Result();
        result.setCode(ResultCodeEnum.FAILED.code);
        result.setMessage(ResultCodeEnum.FAILED.getMessage());
        return result;
    }
    public static Result fail(String message){
        Result result=new Result();
        result.setCode(ResultCodeEnum.FAILED.code);
        result.setMessage(message);
        return result;
    }
    public static Result fail(ResultCodeEnum resultCodeEnum){
        Result result=new Result();
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }
    public static <T> Result noLogin(){
        Result result=new Result();
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage("用户未登录");
        return result;
    }
    public static  Result fail(int code,String errMsg){
        Result result=new Result();
        result.setCode(code);
        result.setMessage(errMsg);
        return result;
    }

}
